package com.example.online.service;

import com.example.online.dao.OnLineDao;
import com.example.online.entity.User;
import com.example.online.utils.MybatisUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;

public class UserService {


    public int isExistByName(String name){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        int num = onLineDao.isExistByName(name);
        MybatisUtils.closeSession(ss);
        return num;
    }

    public int isExistByPhone(String phone){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        int num = onLineDao.isExistByPhone(phone);
        MybatisUtils.closeSession(ss);
        return num;
    }

    public int isLock(String name){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        int num = onLineDao.isLock(name);
        MybatisUtils.closeSession(ss);
        return num;

    }

    public int login(String name,String pwd){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        int num = onLineDao.login(name,pwd);
        MybatisUtils.closeSession(ss);
        return num;
    }

    public void updateLoginNumberAdd1(String name){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        onLineDao.updateLoginNumberAdd1(name);
        MybatisUtils.closeSession(ss);
    }

    public void updateLoginErrorAdd1(String name){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        onLineDao.updateLoginErrorAdd1(name);
        MybatisUtils.closeSession(ss);
    }

    public int findLoginError(String name){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        int num = onLineDao.findLoginError(name);
        MybatisUtils.closeSession(ss);
        return num;

    }

    public void updateLoginError0(String name){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        onLineDao.updateLoginError0(name);
        MybatisUtils.closeSession(ss);
    }

    public void lockOfName(String name){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        onLineDao.lockOfName(name);
        MybatisUtils.closeSession(ss);
    }

    //插入数据代表用户注册成功
    public void insert(String phone,String name,String pwd){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        onLineDao.insert(phone, name, pwd);
        MybatisUtils.closeSession(ss);
    }

    public User getUser(String name,String pwd){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        User user = onLineDao.getUser(name,pwd);
        MybatisUtils.closeSession(ss);

        return user;
    }

    public int updatePwdByPhone(String pwd,String phone){
        SqlSession ss = MybatisUtils.getSession();
        OnLineDao onLineDao = ss.getMapper(OnLineDao.class);
        int num = onLineDao.updatePwdByPhone(pwd,phone);
        MybatisUtils.closeSession(ss);
        return num;
    }
}
